Passed
Push — add-combined-id-support ( 311a2d )
by Eric
01:02
created

describe(ꞌutilities.toObjectꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
var utilities = require('../utilities.js');
2
3
describe('utilities.toObject', function() {
4
    describe('failing tests', function() {
5
        it("should fail because the arr parameter is undefined", function() {
6
            expect(utilities.toObject).toThrowError(TypeError);
7
        });
8
9
        it("should fail because the arr parameter is not an Array", function() {
10
            expect(utilities.toObject.bind(null, {})).toThrowError(TypeError);
11
        });
12
13
        it("should fail because the mapBy not of type String, Array or Function and not falsy", function() {
14
            expect(utilities.toObject.bind(null, [], {})).toThrowError(TypeError);
15
        });
16
    });
17
18
    describe('array of primitives toObject', function() {
19
        // test example from README.md
20
        var states = ['Sachsen', 'Sachsen-Anhalt', 'Berlin', 'Hamburg'];
21
        var statesObject = utilities.toObject(states);
22
        // end test example from README.md
23
24
        it("should be able to access object by index", function() {
25
            expect(statesObject[0]).toEqual('Sachsen');
26
        });
27
28
        it("should have 4 keys", function() {
29
            expect(Object.keys(statesObject).length).toEqual(4);
30
        });
31
    });
32
33
    var news = [
34
        {
35
            id: 12001,
36
            headline: 'Tiger goes limp',
37
            subHeadline: 'Pulls out after 9 holes'
38
        },{
39
            id: 666,
40
            headline: 'Croc has beef with cow',
41
            subHeadline: ''
42
        },{
43
            id: 1337,
44
            headline: 'Germans wurst at penalties',
45
            subHeadline: 'New stats prove England are better from the spot'
46
        }
47
    ];
48
49
    describe('array of objects toObject - string mapBy', function() {
50
        var newsObject = utilities.toObject(news, 'id');
51
52
        it("should be able to access object by ID", function() {
53
            expect(newsObject[1337].headline).toEqual('Germans wurst at penalties');
54
        });
55
56
        it("should have 3 keys", function() {
57
            expect(Object.keys(newsObject).length).toEqual(3);
58
        });
59
    });
60
61
    describe('array of objects toObject - array mapBy', function() {
62
        var newsObject = utilities.toObject(news, ['id', 'id']);
63
64
        it("should be able to access object by ID_ID", function() {
65
            expect(newsObject['1337_1337'].headline).toEqual('Germans wurst at penalties');
66
        });
67
68
        it("should have 3 keys", function() {
69
            expect(Object.keys(newsObject).length).toEqual(3);
70
        });
71
    });
72
73
    describe('array of objects toObject - function mapBy', function() {
74
        var newsObject = utilities.toObject(news, function(val, i, arr) {
0 ignored issues
show
Unused Code introduced by
The parameter arr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
75
            return val.id + '_' + i;
76
        });
77
78
        it("should be able to access object by ID_index", function() {
79
            expect(newsObject['1337_2'].headline).toEqual('Germans wurst at penalties');
80
        });
81
82
        it("should have 3 keys", function() {
83
            expect(Object.keys(newsObject).length).toEqual(3);
84
        });
85
    });
86
});
87